package com.poetize.plus.utils;

import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.http.HttpStatus;

/**
 * @author zuosy
 * @created 2024/3/28 15:47
 */
@Schema(description = "统一响应类")
public record R<T>(
        @Schema(description = "响应编码")
        int code,

        @Schema(description = "响应信息")
        String msg,

        @Schema(description = "响应数据")
        T data
) {

    public static <T> R<T> ok() {
        return restResult(null, HttpStatus.OK.value(), null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, HttpStatus.OK.value(), null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, HttpStatus.OK.value(), msg);
    }

    public static <T> R<T> fail() {
        return restResult(null, HttpStatus.INTERNAL_SERVER_ERROR.value(), null);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), null);
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    public static <T> R<T> failForCodeMsg(int code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        return new R<>(code, msg, data);
    }
}